package com.boarsoft.boar.sys.biz;

import java.util.List;

import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.boarsoft.boar.sys.SysUserFuncBiz;
import com.boarsoft.boar.sys.entity.SysFunc;
import com.boarsoft.common.Util;
import com.boarsoft.common.dao.PagedResult;
import com.boarsoft.hibernate.biz.SimpleBizImpl;

@Component("sysUserFuncBiz")
public class SysUserFuncBizImpl extends SimpleBizImpl implements SysUserFuncBiz {
	@Override
	@Transactional(readOnly = true)
	public PagedResult<SysFunc> list(String userId, String roleId, String key, String orderBy, int pageNo, int pageSize) {
		StringBuffer sb = new StringBuffer();
		sb.append(" from ").append(SysFunc.class.getName());
		sb.append(" f join f.roles r join r.users u where u.id='");
		sb.append(userId).append("' and r.status=").append(SysFunc.STATUS_INUSE);
		if (Util.strIsNotEmpty(roleId)) {
			sb.append(" and r.id='").append(roleId).append("'");
		}
		if (Util.strIsNotEmpty(key)) {
			sb.append(" and (f.code like '%").append(key);
			sb.append("%' or f.name like '%").append(key);
			sb.append("%')");
		}
		int total = Util.object2int(dao.getValue("select count(f.code)".concat(sb.toString())), 0);
		sb.append(" order by f.code");
		// log.info(sb.toString());
		List<SysFunc> lt = dao.list("select f".concat(sb.toString()), pageNo, pageSize);
		return new PagedResult<SysFunc>(total, lt, pageNo, pageSize);
	}
}